package com.frame.easy.modular.activiti.controller;

import com.frame.easy.base.controller.BaseController;
import com.frame.easy.modular.activiti.constant.VariableConst;
import com.frame.easy.modular.activiti.service.ProcessDefinitionService;
import com.frame.easy.result.Tips;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 启动流程
 *
 * @author tengchong
 * @date 2020/3/26
 */
@Controller
@RequestMapping("/auth/activiti/process/definition")
public class ProcessDefinitionController extends BaseController {

    /**
     * view 路径
     */
    private static final String PREFIX = "modular/activiti/process/definition/";

    @Autowired
    private ProcessDefinitionService service;

    /**
     * 读取流程表单
     *
     * @param model               model
     * @param processDefinitionId 流程id
     * @param businessKey         业务数据ID
     * @param businessTitle       业务标题
     * @param businessDetailsUrl  业务数据详情url
     * @return view
     */
    @RequestMapping("/read/start/form/{processDefinitionId}")
    public String readStartForm(Model model, @PathVariable("processDefinitionId") String processDefinitionId,
                                @RequestParam(value = VariableConst.BUSINESS_KEY, required = false) String businessKey,
                                @RequestParam(value = VariableConst.BUSINESS_TITLE, required = false) String businessTitle,
                                @RequestParam(value = VariableConst.BUSINESS_DETAILS_URL, required = false) String businessDetailsUrl) {
        model.addAttribute(VariableConst.BUSINESS_KEY, businessKey);
        model.addAttribute(VariableConst.BUSINESS_TITLE, businessTitle);
        model.addAttribute(VariableConst.BUSINESS_DETAILS_URL, businessDetailsUrl);
        service.readStartForm(model, processDefinitionId);
        return PREFIX + "start-form";
    }

    /**
     * 启动流程
     *
     * @param processDefinitionId 流程id
     * @param businessKey         业务id
     * @param businessTitle       业务标题
     * @return Tips
     */
    @RequestMapping("/start/process/instance/{processDefinitionId}")
    @ResponseBody
    @RequiresPermissions("activiti:process:start")
    public Tips startProcessInstance(@PathVariable("processDefinitionId") String processDefinitionId,
                                     @RequestParam(value = VariableConst.BUSINESS_KEY, required = false) String businessKey,
                                     @RequestParam(value = VariableConst.BUSINESS_TITLE, required = false) String businessTitle,
                                     @RequestParam(value = VariableConst.BUSINESS_DETAILS_URL, required = false) String businessDetailsUrl,
                                     HttpServletRequest request) {
        return Tips.getSuccessTips("操作成功", service.startProcessInstance(processDefinitionId, businessKey, businessTitle, businessDetailsUrl, request));
    }

    /**
     * 尝试启动流程
     * （先验证有无动态表单，如果没有自动提交返回流水号，如果有返回表单地址）
     *
     * @param processDefinitionId 流程id
     * @param businessKey         业务数据ID
     * @param businessTitle       业务标题
     * @param businessDetailsUrl  业务数据详情url
     * @param extentParams        扩展参数(注意此参数必须是JSON格式)
     * @return Tips
     */
    @RequestMapping("/attempt/start/process/instance/{processDefinitionId}")
    @ResponseBody
    public Tips startProcessInstance(@PathVariable("processDefinitionId") String processDefinitionId,
                                     @RequestParam(value = VariableConst.BUSINESS_KEY, required = false) String businessKey,
                                     @RequestParam(value = VariableConst.BUSINESS_TITLE, required = false) String businessTitle,
                                     @RequestParam(value = VariableConst.BUSINESS_DETAILS_URL, required = false) String businessDetailsUrl,
                                     @RequestParam(required = false) String extentParams) {
        return Tips.getSuccessTips("操作成功", service.startProcessInstance(processDefinitionId, businessKey, businessTitle, businessDetailsUrl, extentParams));
    }
}
